<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/8/25
 * Time: 10:29
 */

class Platform extends AppModel
{
    public $name = 'Platform';
    public $useTable = 'capital_assign';
    public $primaryKey = 'id';

    /*
     * 轮播图
     */
    public function countRotationAll($conditions)
    {
        $this->setSource('rotation');
        $count = $this->find('count', array(
            'conditions' => $conditions
        ));
        return $count;
    }

    public function findRotationAll($page, $limit, $conditions)
    {
        $this->setSource('rotation');
        $res = $this->find('all', array(
            'conditions' => $conditions,
            'page' => $page,
            'limit' => $limit
        ));
        return $res;
    }

    public function saveRotation($params)
    {
        $this->setSource('rotation');
        $res = $this->save($params);
        return $res;
    }

    public function delRotation($id)
    {
        $this->setSource('rotation');
        $res = $this->delete($id);
        return $res;
    }

    public function findAllNav($page,$limit,$conditions)
    {
        $this->setSource('navigation');
        $res =  $this->find('all',array(
            'conditions' => $conditions,
            'page' => $page,
            'limit' => $limit,
            'order' => array('Platform.update_time desc'),
        ));
        return $res;
    }

    public function findAllCountNav($conditions)
    {
        $this->setSource('navigation');
        $res =  $this->find('count',array(
            'conditions' => $conditions,
        ));
        return $res;
    }

    public function findOneNav($conditions)
    {
        $this->setSource('navigation');
        $res =  $this->find('first',array(
            'conditions' => $conditions,
        ));
        return $res;
    }

    public function dellNav($data)
    {
        $this->setSource('navigation');
        $res =  $this->save($data);
        return $res;
    }

    public function dropNav($id)
    {
        $this->setSource('navigation');
        $res =  $this->delete($id);
        return $res;
    }

    public function findAllProp($conditions, $page, $limit)
    {
        $this->setSource('prop');
        $result = $this->find('all', array(
            'conditions' => $conditions,
            'page' => $page,
            'limit' => $limit,
            'order' => array('Platform.last_time desc'),
        ));
        return array_column($result,'Platform');
    }

    public function countAllProp($conditions)
    {
        $this->setSource('prop');
        $count = $this->find('count',array(
            'conditions' => $conditions
        ));
        return $count;
    }

    public function dellProp($data)
    {
        $this->setSource('prop');
        $res = $this->save($data);
        return $res;
    }

    public function delProp($id)
    {
        $this->setSource('prop');
        return $this->delete($id);
    }

    public function dellActivityRate($data)
    {
        $this->setSource('activity_award_config');
        return $this->save($data);
    }

    public function getAllActivityRate($conditions)
    {
        $this->setSource('activity_award_config');
        $res = $this->find('all',[
            'fields' => ['id','prid','rate','type'],
            'conditions' => $conditions,
            'order' => 'prid desc',
        ]);
        return array_column($res,'Platform');
    }

    public function findPropNameByIds($conditions)
    {
        $this->setSource('prop');
        $res = $this->find('all',[
            'fields' => ['id','prop_name'],
            'conditions' => $conditions,
        ]);
        return array_column($res,'Platform');
    }

    public function existActivityRate($activity_type,$type,$prop_id)
    {
        $this->setSource('activity_award_config');
        $res = $this->find('count',[
            'conditions' => ['activity_type'=>$activity_type,'type'=>$type,'prid'=>$prop_id],
        ]);
        return $res;
    }

    public function dropNextBoxRate($id)
    {
        $this->setSource('activity_award_config');
        return $this->delete($id);
    }

    public function getAllRate($conditions)
    {
        $this->setSource('activity_award_config');
        $res = $this->find('all', [
            'fields' => ['sum(rate) sum'],
            'conditions' => $conditions
        ]);
        return $res[0][0]['sum'];
    }

    public function getMagicAssign(){
        $this->setSource('capital_assign');
        $rows = $this->find('all', array(
            'fields'     => array('assign_name','assign_value'),
            'conditions' => ['assign_name'=>['blidbox_getmagicbox','login_getmagicbox','signin_getmagicbox']],
        ));
        foreach ($rows as $key => $value){
            $res[$value['Platform']['assign_name']] = $value['Platform']['assign_value'];
        }
        return $res;
    }

    public function getAllProps($conditions)
    {
        $this->setSource('prop');
        return $this -> find('all', array(
            'fields' => 'id, prop_name',
            'conditions' => $conditions
        ));
    }

    public function updatesafetyassign($info)
    {
        $this->setSource('capital_assign');
        $res = $this->updateAll(['assign_value' => $info['assign_value']],['assign_name' => $info['assign_name']]);
//        $sql = "UPDATE capital_assign set assign_value = '{$info['assign_value']}' where assign_name = '{$info['assign_name']}'";
//        return $this->query($sql);
        return $res ? $this->getAffectedRows() : false;
    }

}